package com.application.outdoor.adapter;

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import ReadWriteXML.XMLBuilder;

import com.application.outdoor.pojo.CayThaoLuanPOJO;
import com.application.outdoor.pojo.NhomPOJO;



public class SubjectList {
	//public static String test="<Root><Nhom ID=\"1\" TenNhom=\"Nh�m 1\"><Cay ID=\"1\" IDNguoiTao=\"1\" IDNhom=\"1\" NoiDung=\"H?c c?ng t�c l� g�?\" version=\"1341762933000\"/><Cay ID=\"4\" IDNguoiTao=\"1\" IDNhom=\"1\" NoiDung=\"tào lao\" version=\"1341676384000\"/></Nhom><Nhom ID=\"2\" TenNhom=\"Nh�m 2\"><Cay ID=\"2\" IDNguoiTao=\"1\" IDNhom=\"2\" NoiDung=\"T?t r?t vui\" version=\"1341802284000\"/></Nhom></Root>";
	private ArrayList<SubjectEntity> data;
	public ArrayList<SubjectEntity> getData() {
		return data;
	}

	public void setData(ArrayList<SubjectEntity> data) {
		this.data = data;
	}
	
	public CayThaoLuanPOJO getMember(int groupIndex, int subjectIndex){
		return this.data.get(groupIndex).getLstCay().get(subjectIndex);
	}
	public NhomPOJO getGroup(int groupIndex){
		return this.data.get(groupIndex).getNhom();
	}
	public void addCay(int idNhom, CayThaoLuanPOJO cay){
	
		for(int i=0;i<this.data.size();i++){
			if(this.data.get(i).getNhom().getId()==idNhom){
				this.data.get(i).getLstCay().add(cay);
			}
		}		
	}
	public int getSize(){
		return this.data.size();
	}
	public int getChildSize(int index){
		return this.data.get(index).getLstCay().size();
	}
	public void init(String xml){
		this.data=new ArrayList<SubjectEntity>();
		
		try{
		Document doc=XMLBuilder.loadXMLFromString(xml);
		NodeList groupList=doc.getElementsByTagName("Nhom");
		
		if(groupList!=null){
			for(int i=0;i<groupList.getLength();i++){
				
				Element group=(Element) groupList.item(i);
				NodeList memberList=group.getChildNodes();
				
				NhomPOJO nhom=new NhomPOJO();
				ArrayList<CayThaoLuanPOJO> lstCay=new ArrayList<CayThaoLuanPOJO>();
				
				nhom.setId(Integer.parseInt(group.getAttribute("ID")));
				nhom.setTenNhom(group.getAttribute("TenNhom"));
				
				for(int j=0;j<memberList.getLength();j++){
					CayThaoLuanPOJO cay=new CayThaoLuanPOJO();
					Element cayElement=(Element) memberList.item(j);
					cay.getFromXMLElement(cayElement);
					lstCay.add(cay);
				}
				this.data.add(new SubjectEntity(nhom, lstCay));
			}
		}
		}catch(Exception e){
			
		}
	}
}
